package com.ids.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ids.bean.UserAttendanceData;
import com.ids.service.AttendanceScreenService;
import com.ids.util.Constants;
import com.ids.util.Utlity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AttendanceScreen extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private Map<Integer,String> attendanceType;
	private Map<Integer,String> dependantAttendanceType;
	private int attendanceTypeId;
	private int dependantAttendanceTypeId;

	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		String empCode = (String) session.get("empCode");
		Date currentDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String currentFormattedDate = formatter.format(currentDate);
		UserAttendanceData userAttendance = AttendanceScreenService.getInstance().getSelectedAttendance(currentFormattedDate,empCode);
		setAttendanceTypeId(userAttendance.getAttendanceTypeId());
		setDependantAttendanceTypeId(userAttendance.getDependantAttendanceTypeId());
		attendanceType = AttendanceScreenService
				.getInstance().getAttendanceType(empCode);
		Map<Integer,String> dependantAttendanceTypes = AttendanceScreenService
			.getInstance().getDependantAttendanceType(userAttendance.getAttendanceTypeId(),empCode);
		setDependantAttendanceType(dependantAttendanceTypes);
		Map<Integer,String> noOFDays = getNoOfDays();
		request.setAttribute("noOFDays",noOFDays);
		request.setAttribute("dependantAttendanceTypes",dependantAttendanceTypes);
		request.setAttribute("attendanceTypes", attendanceType);
		String userAttendanceMessage = (String) session
				.get(Constants.USER_ATTENDANCE_MESSAGE);
		request.setAttribute(Constants.USER_ATTENDANCE_MESSAGE,
				userAttendanceMessage);
		session.remove(Constants.USER_ATTENDANCE_MESSAGE);
		return SUCCESS;
	}

	public String getDependentAttendanceType() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String empCode = (String) session.get("empCode");
		HttpServletRequest request = ServletActionContext.getRequest();
		String forward = "success";
		int attendanceValue = Integer.parseInt(request.getParameter("attendanceTypeValue"));
		Map<Integer,String> dependantAttendanceTypes = AttendanceScreenService
		.getInstance().getDependantAttendanceType(attendanceValue, empCode);
		setDependantAttendanceType(dependantAttendanceTypes);
		request.setAttribute("dependantAttendanceTypes",dependantAttendanceTypes);
		return forward;
	}

	

	public Map<Integer, String> getAttendanceType() {
		return attendanceType;
	}

	public void setAttendanceType(Map<Integer, String> attendanceType) {
		this.attendanceType = attendanceType;
	}

	public Map<Integer, String> getDependantAttendanceType() {
		return dependantAttendanceType;
	}

	public void setDependantAttendanceType(
			Map<Integer, String> dependantAttendanceType) {
		this.dependantAttendanceType = dependantAttendanceType;
	}

	public int getAttendanceTypeId() {
		return attendanceTypeId;
	}

	public void setAttendanceTypeId(int attendanceTypeId) {
		this.attendanceTypeId = attendanceTypeId;
	}

	public int getDependantAttendanceTypeId() {
		return dependantAttendanceTypeId;
	}

	public void setDependantAttendanceTypeId(int dependantAttendanceTypeId) {
		this.dependantAttendanceTypeId = dependantAttendanceTypeId;
	}
	public Map<Integer,String> getNoOfDays() {
		return Utlity.getNoOfDays();
	}
	
	

}
